package com.mg.plugin.dro.module;

import com.mg.model.Model;
import com.mg.model.Param;
import com.mg.model.Port;
import com.mg.plugin.drought.common.PenmamCalculator;
import com.mg.plugin.drought.common.RelativeHumidityIndexCalculator;
import com.mg.plugin.drought.entity.StationData;
import com.mg.plugin.drought.util.AssistStationData;
import com.mg.pluginexample2.ModuleBase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作物水分亏缺指数
 */
public class CalcualteEt0Module extends ModuleBase {

    public CalcualteEt0Module(int nID) {
        super(nID);
        m_alInputPort.add(new Port(this, Port.Type.Unknown));
        m_alOutputPort.add(new Port(this, Port.Type.Unknown));
    }

    public String GetGroupName() {
        return "干旱制作";
    }

    public String GetName() {
        return "计算ET0";
    }

    public String GetDescription() {
        return "计算彭曼蒸散量";
    }

    public boolean OnAttach(Port portFrom, Port portTo) {
        try {
            int i = FindPort(portFrom, false);
            if (i != -1) {
                return true;
            }
            i = FindPort(portTo, true);
            if (i == -1) {
                return false;
            }

            ArrayList<Param> alParam = new ArrayList<Param>();
            portFrom.GetModule().GetOutputParam(portFrom, alParam);

            if (alParam.size() != 1) {
                return false;
            }

            if (alParam.get(0).m_objValue != null && !(alParam.get(0).m_objValue instanceof ArrayList)) {
                m_model.OutputLog(Model.LogLevel.Error, "No ArrayList");
                return false;
            }
            if (i == 0) {
                m_model.OutputLog(Model.LogLevel.Info, "m_inputForecastList" + alParam.get(0).m_objValue.getClass());
                m_inputForecastList = (List<String>) alParam.get(0).m_objValue;
            }

            portTo.SetType(portFrom.GetType());
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean OnDetach(Port portFrom, Port portTo) {
        if (!super.OnDetach(portFrom, portTo))
            return false;
        try {
            int i = FindPort(portTo, true);
            if (i == -1)
                return true;
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    //用于 参数->XML 等
    public int GetParam(ArrayList<Param> alParam) {
        try {
            super.GetParam(alParam);
            alParam.add(new Param("StationId", m_strStationId, "站点ID", "StationId", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Date", m_strDate, "数据时间", "Date", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Lat", m_strLat, "站点纬度", "Lat", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Alti", m_strAlti, "海拔高度", "Alti", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("AvgTemp", m_strAvgTemp, "平均温度", "AvgTemp", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("MaxTemp", m_strMaxTemp, "最高温度", "MaxTemp", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("MinTemp", m_strMinTemp, "最低温度", "MinTemp", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("WindSpeed", m_strWindSpeed, "风速", "WindSpeed", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Rain", m_strRain, "降水量", "Rain", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Humidity", m_strHumidity, "湿度", "Humidity", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Sunshine", m_strSunshine, "日照", "Sunshine", "参数匹配", Param.EditType.Default));

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return alParam.size();
    }

    @Override
    public boolean OnParamChanged(ArrayList<Param> alParam, int nIndex, Object objValue) {
        return super.OnParamChanged(alParam, nIndex, objValue);
    }

    //用于 XML->参数 等
    @Override
    public int SetParam(final ArrayList<Param> alParam) {
        int i = super.SetParam(alParam);
        try {
            m_strStationId = (String) alParam.get(i++).m_objValue;
            m_strDate = (String) alParam.get(i++).m_objValue;
            m_strLat = (String) alParam.get(i++).m_objValue;
            m_strAlti = (String) alParam.get(i++).m_objValue;
            m_strAvgTemp = (String) alParam.get(i++).m_objValue;
            m_strMaxTemp = (String) alParam.get(i++).m_objValue;
            m_strMinTemp = (String) alParam.get(i++).m_objValue;
            m_strWindSpeed = (String) alParam.get(i++).m_objValue;
            m_strRain = (String) alParam.get(i++).m_objValue;
            m_strHumidity = (String) alParam.get(i++).m_objValue;
            m_strSunshine = (String) alParam.get(i++).m_objValue;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return i;
    }

    @Override
    public int GetOutputParam(Port port, ArrayList<Param> alParam) {
        int i = FindPort(port, false);
        if (i == -1) {
            System.out.println("Post Is Null");
            return 0;
        }

        alParam.add(new Param("ResultString", m_listCSV));
        return alParam.size();
    }

    @Override
    @SuppressWarnings("static-access")
    public boolean Execute() {

        m_listCSV.clear();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String head = "id,stationId,day,date,et0,lat,alti,avgTemp,tmax,tmin,windSpeed,sunshine,humidity";
        String type = "String,String,Double,String,Double,Double,Double,Double,Double,Double,Double,Double,Double";
        m_listCSV.add(head);
        m_listCSV.add(type);

        Map<String, String> headerIndex = new HashMap<String, String>();
        headerIndex.put("StationId", m_strStationId);
        headerIndex.put("Date", m_strDate);
        headerIndex.put("Lat", m_strLat);
        headerIndex.put("Alti", m_strAlti);
        headerIndex.put("AvgTemp", m_strAvgTemp);
        headerIndex.put("MaxTemp", m_strMaxTemp);
        headerIndex.put("MinTemp", m_strMinTemp);
        headerIndex.put("WindSpeed", m_strWindSpeed);
        headerIndex.put("Rain", m_strRain);
        headerIndex.put("Humidity", m_strHumidity);
        headerIndex.put("Sunshine", m_strSunshine);


        Calendar calendar = Calendar.getInstance();

        try {
            List<StationData> newStationDataList = AssistStationData.assistStationData(headerIndex, m_inputForecastList);
            for (StationData stationData : newStationDataList) {
                Date date = sdf.parse(stationData.getDate());
                calendar.setTime(date);
                stationData.setDay(calendar.get(6));
                double g = PenmamCalculator.caleEtref(stationData, this.m_model);
                this.m_listCSV.add(stationData.getStationId() + "/" + sdf.format(date) + "," + stationData.getStationId() + ","
                        + stationData.getDay() + "," + sdf.format(date) + "," + g + "," + stationData.getLat() + ","
                        + stationData.getAlti() + "," + stationData.gettAvg() + "," + stationData.gettMax() + ","
                        + stationData.gettMin() + "," + stationData.getWindSpeed() + "," + stationData.getSunshine()
                        + "," + stationData.getHumidity());
            }
        } catch (ParseException var12) {
            return false;
        }

        OnParamChanged();
        return true;
    }

    private List<String> m_inputForecastList = new ArrayList<String>();
    private List<String> m_listCSV = new ArrayList<String>();

    private String m_strStationId;
    private String m_strDate;
    private String m_strLat;
    private String m_strAlti;
    private String m_strAvgTemp;
    private String m_strMaxTemp;
    private String m_strMinTemp;
    private String m_strWindSpeed;
    private String m_strRain;
    private String m_strHumidity;
    private String m_strSunshine;

}
